####2019
# electricity consumption data at MSOA level (pre downloaded)
inFile <-paste0(dataFolder, "energy/electricity/MSOA Dom Elec csv/MSOA_DOM_ELEC_2019.csv")
msoa_elecData <- readr::read_csv(inFile)
##
## -- Column specification --------------------------------------------------------
## cols(
## `Local Authority Name` = col_character(),
## `Local Authority Code` = col_character(),
## `MSOA Name` = col_character(),
## `Middle Layer Super Output Area (MSOA) Code` = col_character(),
## `Number of meters` = col_double(),
## `Consumption (kWh)` = col_double(),
## `Mean consumption (kWh per meter)` = col_double(),
## `Median consumption (kWh per meter)` = col_double()
## )
head(msoa_elecData)
## # A tibble: 6 x 8
## `Local Authority N~ `Local Authority ~ `MSOA Name` `Middle Layer Super Outpu~
## <chr> <chr> <chr> <chr>
## 1 Hartlepool E06000001 Hartlepool ~ E02002483
## 2 Hartlepool E06000001 Hartlepool ~ E02002484
## 3 Hartlepool E06000001 Hartlepool ~ E02002485
## 4 Hartlepool E06000001 Hartlepool ~ E02002487
## 5 Hartlepool E06000001 Hartlepool ~ E02002488
## 6 Hartlepool E06000001 Hartlepool ~ E02002489
## # ... with 4 more variables: Number of meters <dbl>, Consumption (kWh) <dbl>,
## # Mean consumption (kWh per meter) <dbl>,
## # Median consumption (kWh per meter) <dbl>
inf <-paste0(dataFolder, "boundaries/MSOA/msoa_solent.shp")
message("Loading Middle Layer Super Output Area (MSOA) from file")
## Loading Middle Layer Super Output Area (MSOA) from file
#When using shp files you might also need to have other supplementary files in the same folder to allow it to run https://cfss.uchicago.edu/notes/simple-features/
msoa_sf_data <- sf::read_sf(inf)
head (msoa_sf_data)
## Simple feature collection with 6 features and 13 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 458985.4 ymin: 102894 xmax: 467725.4 ymax: 107035
## Projected CRS: OSGB 1936 / British National Grid
## # A tibble: 6 x 14
## MSOA11CD MSOA11NM OBJECTID BNG_E BNG_N LONG_ LAT Shape_Leng Shape__Are
## <chr> <chr> <int> <int> <int> <dbl> <dbl> <dbl> <dbl>
## 1 E02003524 Portsmouth~ 3510 463726 106331 -1.10 50.9 8175. 1880016.
## 2 E02003525 Portsmouth~ 3511 465172 105904 -1.08 50.8 13220. 2012966.
## 3 E02003526 Portsmouth~ 3512 466581 106095 -1.06 50.9 10425. 2260228.
## 4 E02003527 Portsmouth~ 3513 464176 104420 -1.09 50.8 21957. 3838768.
## 5 E02003529 Portsmouth~ 3514 466420 104925 -1.06 50.8 10636. 1499618.
## 6 E02003530 Portsmouth~ 3515 465411 103721 -1.07 50.8 13264. 1695461.
## # ... with 5 more variables: Shape__Len <dbl>, LAD11CD <chr>, LAD11NM <chr>,
## # nOAs <int>, geometry <MULTIPOLYGON [m]>
table(msoa_sf_data$LAD11NM)#how many MSOAs are in each LA
##
## Basingstoke and Deane East Hampshire Eastleigh
## 22 15 15
## Fareham Gosport Hart
## 14 10 11
## Havant Isle of Wight New Forest
## 17 18 23
## Portsmouth Southampton Test Valley
## 25 32 15
## Winchester
## 14
msoa_elecData$MSOA11CD <- msoa_elecData$ 'Middle Layer Super Output Area (MSOA) Code' #Creating a variable with the LA code and the name as in the sf_data
msoa_merged_sf <-merge(msoa_sf_data, msoa_elecData)
ggplot2::ggplot(msoa_merged_sf) +
geom_sf(aes(fill = `Mean consumption (kWh per meter)`)) +
scale_fill_continuous(name = "Electricity: Mean kWh per meter", low = "green", high = "red") +
labs(caption = "Solent (all MSOAs)")
#mapping it
####Mapping the change from 2010 to 2019
domelec_MSOA_2010<-readr::read_csv(paste0(dataFolder,"energy/electricity/MSOA Dom Elec csv/MSOA_DOM_ELEC_2010.csv"),skip = 1)
##
## -- Column specification --------------------------------------------------------
## cols(
## `Local Authority Name` = col_character(),
## `Local Authority Code` = col_character(),
## `MSOA Name` = col_character(),
## `Middle Layer Super Output Area (MSOA) Code` = col_character(),
## `Number of meters` = col_double(),
## `Consumption (kWh)` = col_double(),
## `Mean consumption (kWh per meter)` = col_double(),
## `Median consumption (kWh per meter)` = col_double()
## )
domelec_MSOA_2019<-readr::read_csv(paste0(dataFolder, "energy/electricity/MSOA Dom Elec csv/MSOA_DOM_ELEC_2019.csv"))
##
## -- Column specification --------------------------------------------------------
## cols(
## `Local Authority Name` = col_character(),
## `Local Authority Code` = col_character(),
## `MSOA Name` = col_character(),
## `Middle Layer Super Output Area (MSOA) Code` = col_character(),
## `Number of meters` = col_double(),
## `Consumption (kWh)` = col_double(),
## `Mean consumption (kWh per meter)` = col_double(),
## `Median consumption (kWh per meter)` = col_double()
## )
domelec_MSOA_2010$domelec2010<-domelec_MSOA_2010$'Mean consumption (kWh per meter)'
domelec_MSOA_2019$domelec2019<-domelec_MSOA_2019$'Mean consumption (kWh per meter)'
merged_domelec<-merge(domelec_MSOA_2010, domelec_MSOA_2019, by="Middle Layer Super Output Area (MSOA) Code", all=TRUE)
names(merged_domelec)
## [1] "Middle Layer Super Output Area (MSOA) Code"
## [2] "Local Authority Name.x"
## [3] "Local Authority Code.x"
## [4] "MSOA Name.x"
## [5] "Number of meters.x"
## [6] "Consumption (kWh).x"
## [7] "Mean consumption (kWh per meter).x"
## [8] "Median consumption (kWh per meter).x"
## [9] "domelec2010"
## [10] "Local Authority Name.y"
## [11] "Local Authority Code.y"
## [12] "MSOA Name.y"
## [13] "Number of meters.y"
## [14] "Consumption (kWh).y"
## [15] "Mean consumption (kWh per meter).y"
## [16] "Median consumption (kWh per meter).y"
## [17] "domelec2019"
merged_domelec$domelec_diff<-merged_domelec$domelec2019 - merged_domelec$domelec2010
summary(merged_domelec)
## Middle Layer Super Output Area (MSOA) Code Local Authority Name.x
## Length:8481 Length:8481
## Class :character Class :character
## Mode :character Mode :character
##
##
##
## Local Authority Code.x MSOA Name.x Number of meters.x
## Length:8481 Length:8481 Min. : 564
## Class :character Class :character 1st Qu.: 2641
## Mode :character Mode :character Median : 3155
## Mean : 3208
## 3rd Qu.: 3717
## Max. :48900
## Consumption (kWh).x Mean consumption (kWh per meter).x
## Min. : 2137713 Min. :2570
## 1st Qu.: 10550980 1st Qu.:3662
## Median : 12869539 Median :3992
## Mean : 13306863 Mean :4171
## 3rd Qu.: 15554049 3rd Qu.:4460
## Max. :191209969 Max. :8817
## Median consumption (kWh per meter).x domelec2010 Local Authority Name.y
## Min. :1882 Min. :2570 Length:8481
## 1st Qu.:3046 1st Qu.:3662 Class :character
## Median :3331 Median :3992 Mode :character
## Mean :3413 Mean :4171
## 3rd Qu.:3679 3rd Qu.:4460
## Max. :6143 Max. :8817
## Local Authority Code.y MSOA Name.y Number of meters.y
## Length:8481 Length:8481 Min. : 534
## Class :character Class :character 1st Qu.: 2740
## Mode :character Mode :character Median : 3302
## Mean : 3385
## 3rd Qu.: 3943
## Max. :47253
## Consumption (kWh).y Mean consumption (kWh per meter).y
## Min. : 1793548 Min. :1896
## 1st Qu.: 9400807 1st Qu.:3123
## Median : 11595695 Median :3408
## Mean : 12113757 Mean :3589
## 3rd Qu.: 14271817 3rd Qu.:3828
## Max. :146881574 Max. :7657
## Median consumption (kWh per meter).y domelec2019 domelec_diff
## Min. :1485 Min. :1896 Min. :-2669.2
## 1st Qu.:2582 1st Qu.:3123 1st Qu.: -670.3
## Median :2804 Median :3408 Median : -557.9
## Mean :2877 Mean :3589 Mean : -582.0
## 3rd Qu.:3083 3rd Qu.:3828 3rd Qu.: -462.9
## Max. :5505 Max. :7657 Max. : 354.3
merged_domelec$MSOA11CD<-merged_domelec$"Middle Layer Super Output Area (MSOA) Code"
msoa_domelec20102019<-merge(msoa_sf_data, merged_domelec)
ggplot2::ggplot(msoa_domelec20102019)+geom_sf(aes(fill=domelec_diff))+scale_fill_continuous(name="Change in Mean comsumpton (kWh per meter)",low="green",high="red")+labs(caption="Solent(all MSOAs)")
### Mapping the Gas
####2019
# gas consumption data at MSOA level (pre downloaded)
inFile <-paste0(dataFolder, "energy/gas/MSOA Dom Gas csv/MSOA_GAS_2019.csv")
msoa_gasData <- readr::read_csv(inFile)
##
## -- Column specification --------------------------------------------------------
## cols(
## `Local Authority Name` = col_character(),
## `Local Authority Code` = col_character(),
## `MSOA Name` = col_character(),
## `Middle Layer Super Output Area (MSOA) Code` = col_character(),
## `Number of consuming meters` = col_double(),
## `Consumption (kWh)` = col_double(),
## `Mean consumption (kWh per meter)` = col_double(),
## `Median consumption (kWh per meter)` = col_double(),
## `Number of non consuming meters` = col_character()
## )
head(msoa_gasData)
## # A tibble: 6 x 9
## `Local Authority N~ `Local Authority ~ `MSOA Name` `Middle Layer Super Outpu~
## <chr> <chr> <chr> <chr>
## 1 Hartlepool E06000001 Hartlepool ~ E02002483
## 2 Hartlepool E06000001 Hartlepool ~ E02002484
## 3 Hartlepool E06000001 Hartlepool ~ E02002485
## 4 Hartlepool E06000001 Hartlepool ~ E02002487
## 5 Hartlepool E06000001 Hartlepool ~ E02002488
## 6 Hartlepool E06000001 Hartlepool ~ E02002489
## # ... with 5 more variables: Number of consuming meters <dbl>,
## # Consumption (kWh) <dbl>, Mean consumption (kWh per meter) <dbl>,
## # Median consumption (kWh per meter) <dbl>,
## # Number of non consuming meters <chr>
msoa_gasData$MSOA11CD <- msoa_gasData$ 'Middle Layer Super Output Area (MSOA) Code'
msoa_merged_sf_domgas <-merge(msoa_sf_data, msoa_gasData)
ggplot2::ggplot(msoa_merged_sf_domgas) +
geom_sf(aes(fill = `Mean consumption (kWh per meter)`)) +
scale_fill_continuous(name = "Gas: Mean kWh per meter", low = "green", high = "red") +
labs(caption = "Solent (all MSOAs)")
#mapping it
Does this match to non-gas areas??
####Mapping the change in gas consumption from 2010 to 2019
#Green indicates a decrease in gas consumption from 2010 to 2019
#Red indicates an increase in gas consumption from 2010 to 2019
domgas_MSOA_2010 <- readr::read_csv(paste0(dataFolder, "energy/gas/MSOA Dom Gas csv/MSOA_GAS_2010.csv"))
##
## -- Column specification --------------------------------------------------------
## cols(
## `Local Authority Name` = col_character(),
## `Local Authority Code` = col_character(),
## `MSOA Name` = col_character(),
## `Middle Layer Super Output Area (MSOA) Code` = col_character(),
## `Number of meters` = col_double(),
## `Consumption (kWh)` = col_double(),
## `Mean consumption (kWh per meter)` = col_double(),
## `Median consumption (kWh per meter)` = col_double()
## )
domgas_MSOA_2019 <- readr::read_csv(paste0(dataFolder, "energy/gas/MSOA Dom Gas csv/MSOA_GAS_2019.csv"))
##
## -- Column specification --------------------------------------------------------
## cols(
## `Local Authority Name` = col_character(),
## `Local Authority Code` = col_character(),
## `MSOA Name` = col_character(),
## `Middle Layer Super Output Area (MSOA) Code` = col_character(),
## `Number of consuming meters` = col_double(),
## `Consumption (kWh)` = col_double(),
## `Mean consumption (kWh per meter)` = col_double(),
## `Median consumption (kWh per meter)` = col_double(),
## `Number of non consuming meters` = col_character()
## )
domgas_MSOA_2010$domgas2010 <- domgas_MSOA_2010$`Mean consumption (kWh per meter)`
domgas_MSOA_2019$domgas2019 <- domgas_MSOA_2019$`Mean consumption (kWh per meter)`
merged_domgas <- merge(domgas_MSOA_2010, domgas_MSOA_2019, by = "Middle Layer Super Output Area (MSOA) Code", all = TRUE)
names(merged_domgas)
## [1] "Middle Layer Super Output Area (MSOA) Code"
## [2] "Local Authority Name.x"
## [3] "Local Authority Code.x"
## [4] "MSOA Name.x"
## [5] "Number of meters"
## [6] "Consumption (kWh).x"
## [7] "Mean consumption (kWh per meter).x"
## [8] "Median consumption (kWh per meter).x"
## [9] "domgas2010"
## [10] "Local Authority Name.y"
## [11] "Local Authority Code.y"
## [12] "MSOA Name.y"
## [13] "Number of consuming meters"
## [14] "Consumption (kWh).y"
## [15] "Mean consumption (kWh per meter).y"
## [16] "Median consumption (kWh per meter).y"
## [17] "Number of non consuming meters"
## [18] "domgas2019"
merged_domgas$mc_diff <- as.numeric(merged_domgas$domgas2019) - merged_domgas$domgas2010
summary(merged_domgas)
## Middle Layer Super Output Area (MSOA) Code Local Authority Name.x
## Length:8379 Length:8379
## Class :character Class :character
## Mode :character Mode :character
##
##
##
##
## Local Authority Code.x MSOA Name.x Number of meters Consumption (kWh).x
## Length:8379 Length:8379 Min. : 6 Min. : 63413
## Class :character Class :character 1st Qu.: 2210 1st Qu.: 31635464
## Mode :character Mode :character Median : 2764 Median : 41053291
## Mean : 2713 Mean : 41122900
## 3rd Qu.: 3299 3rd Qu.: 50354325
## Max. :66591 Max. :911366463
## NA's :6 NA's :6
## Mean consumption (kWh per meter).x Median consumption (kWh per meter).x
## Min. : 7046 Min. : 3702
## 1st Qu.:13457 1st Qu.:12324
## Median :15019 Median :13885
## Mean :15394 Mean :14140
## 3rd Qu.:16884 3rd Qu.:15624
## Max. :37626 Max. :34513
## NA's :6 NA's :6
## domgas2010 Local Authority Name.y Local Authority Code.y
## Min. : 7046 Length:8379 Length:8379
## 1st Qu.:13457 Class :character Class :character
## Median :15019 Mode :character Mode :character
## Mean :15394
## 3rd Qu.:16884
## Max. :37626
## NA's :6
## MSOA Name.y Number of consuming meters Consumption (kWh).y
## Length:8379 Min. : 5 Min. :6.682e+04
## Class :character 1st Qu.: 2333 1st Qu.:2.988e+07
## Mode :character Median : 2904 Median :3.853e+07
## Mean : 2877 Mean :3.882e+07
## 3rd Qu.: 3465 3rd Qu.:4.745e+07
## Max. :87332 Max. :1.006e+09
## NA's :3 NA's :3
## Mean consumption (kWh per meter).y Median consumption (kWh per meter).y
## Min. : 6857 Min. : 5465
## 1st Qu.:11882 1st Qu.:10698
## Median :13281 Median :12043
## Mean :13684 Mean :12328
## 3rd Qu.:15049 3rd Qu.:13608
## Max. :31023 Max. :30263
## NA's :3 NA's :3
## Number of non consuming meters domgas2019 mc_diff
## Length:8379 Min. : 6857 Min. :-17479
## Class :character 1st Qu.:11882 1st Qu.: -2050
## Mode :character Median :13281 Median : -1661
## Mean :13684 Mean : -1707
## 3rd Qu.:15049 3rd Qu.: -1284
## Max. :31023 Max. : 5830
## NA's :3 NA's :9
merged_domgas$MSOA11CD <- merged_domgas$"Middle Layer Super Output Area (MSOA) Code" #creating a variable with the LSOA code in the same name as in sf_data
msoa_domgas20102019 <- merge(msoa_sf_data, merged_domgas) #merging these
ggplot2::ggplot(msoa_domgas20102019) +
geom_sf(aes(fill=mc_diff))+
scale_fill_continuous(name="Change in Gas: Mean kWh per meter", low="green",high="red")+
labs(caption ="Solent (all MSOAs)")
Use leaflet version to place geo tile underlay
First check co-ord system so matches leaflet
# https://dataknut.github.io/mapping-with-r/ONS-open-geography.html
library(leaflet)
st_coord_sys <- st_crs(msoa_domgas20102019) # check coord system
st_coord_sys$epsg # current coord system ? Leaflet wants EPSG: 4326
## [1] 27700
# transform the coord system if required
if(st_coord_sys$epsg != 4326){
map_df_trans <- st_transform(msoa_domgas20102019, "+proj=longlat +datum=WGS84")
}
Plot the leaflet map
#h/t @tom_rushby - see also https://rstudio.github.io/leaflet/colors.html
qpal <- colorNumeric("Reds", map_df_trans$mc_diff, n = 9)
leaflet(map_df_trans) %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addPolygons(color = ~qpal(mc_diff),
fillOpacity = 0.6, weight = 1.5, popup = ~(MSOA11CD), # popups clicked
highlight = highlightOptions(
weight = 5,
color = "#666",
fillOpacity = 0.7,
bringToFront = TRUE))